<?php
    if (! class_exists('NaturalExtensionAjax_PageBuilder')) {
        class NaturalExtensionAjax_PageBuilder {
            private $default_actions;

            public function __construct() {
                $this->default_actions = new NaturalExtension_DefaultActions();
            }

            public function build_page() {
                $page = array(
                    'post_name'     => $this->getData($_POST['post_name']),
                    'post_title'    => $this->getData($_POST['post_title']),
                    'menu_order'    => $this->getData($_POST['menu_order']),
                    'post_content'  => $this->getData($_POST['post_content']),
                    'post_type'     => 'page',
                    'post_status'   => 'publish'
                );
                $menu_title     = $this->getData($_POST['menu_title']);
                $service_type   = $this->getData($_POST['service_type']);
                $last_page      = $this->getData($_POST['last_page'], false);
                $header_menu_id = $this->getData($_POST['header_menu_id'], false);
                $footer_menu_id = $this->getData($_POST['footer_menu_id'], false);
                $message = 'success';

                try {
                    // create the page
                    $new_page_id = wp_insert_post($page, true);

                    // select a template for this page
                    switch ($page['post_name']) {
                        case 'home':
                            $page_template = 'home-page.php';
                            break;
                        case 'contact':
                            $page_template = 'contact.php';
                            break;
                        default:
                            $page_template = 'page-right-sidebar.php';
                            break;
                    }

                    // set the page template
                    update_post_meta($new_page_id, "_wp_page_template", $page_template);

                    // insert the menu items
                    if ($header_menu_id !== false) {
                        $this->update_nav_menu_item($header_menu_id, $menu_title, $page['menu_order'], $new_page_id);
                    }

                    if ($footer_menu_id !== false) {
                        $this->update_nav_menu_item($footer_menu_id, $menu_title, $page['menu_order'], $new_page_id);
                    }
                } catch (Exception $e) {
                    $message = $e->getMessage();
                }

                if ($last_page == 'true') {
                    $this->default_actions->set_theme_defaults($service_type);
                }

                exit($message);
            }

            public function getData($data, $noData = '') {
                return empty($data) ? $noData : $data;
            }

            public function update_nav_menu_item($menu_id, $menu_title, $menu_order, $page_id) {
                wp_update_nav_menu_item($menu_id, 0, array(
                    'menu-item-title'       => $menu_title,
                    'menu-item-parent-id'   => 0,
                    'menu-item-position'    => $menu_order,
                    'menu-item-object'      => 'page',
                    'menu-item-object-id'   => $page_id,
                    'menu-item-url'         => get_page_link($page_id),
                    'menu-item-type'        => 'post_type',
                    'menu-item-status'      => 'publish'
                ));
            }
        }

        $natext_ajax_page_builder = new NaturalExtensionAjax_PageBuilder();

        add_action('wp_ajax_nopriv_NatExtAjax_BuildPages', array($natext_ajax_page_builder, 'build_page'));
        add_action('wp_ajax_NatExtAjax_BuildPages', array($natext_ajax_page_builder, 'build_page'));
    }
?>